package ytdp.careagle.com.ytdp.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.os.Message;
import android.util.Config;
import android.util.Log;
import android.view.WindowManager;

import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import org.litepal.LitePal;
import org.litepal.LitePalApplication;

import java.util.ArrayList;
import java.util.List;

import ytdp.careagle.com.ytdp.activity.CarEvaluateQueryActivity;
import ytdp.careagle.com.ytdp.activity.ForgetPasswordActivity;
import ytdp.careagle.com.ytdp.activity.LoginActivity;
import ytdp.careagle.com.ytdp.activity.MainActivity;
import ytdp.careagle.com.ytdp.activity.TestActivity;
import ytdp.careagle.com.ytdp.activity.WelcomeActivity;
import ytdp.careagle.com.ytdp.entity.ProvinceEntity;


/**
 * Created by Administrator on 2017/4/12 0012.
 */

public class APP extends LitePalApplication {

    private static APP mInstance;
    private Handler mHandler;
    private List<Activity> activityList = new ArrayList<>();//所有activity栈
    private static final long delayTime = 2 * 60 * 1000;// 延迟跳转到首页到时间

    public static APP getInstance() {
        return mInstance;
    }

    public List<Activity> getActivityList() {
        return activityList;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        G.APPVERSION = getVersion();
        initOtherSdk();
        initWithHeight();
        startCountDown();
        initUmeng();
    }

    private void initUmeng() {
        MobclickAgent.setScenarioType(this, MobclickAgent.EScenarioType.E_DUM_NORMAL);
        UMConfigure.setLogEnabled(true);
        //初始化组件化基础库, 统计SDK/推送SDK/分享SDK都必须调用此初始化接口
//        UMConfigure.init(this, "59edc54eaed1792a07000023", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "669c30a9584623e70e8cd01b0381dcb4");
        //开启ShareSDK debug模式，方便定位错误，具体错误检查方式可以查看http://dev.umeng.com/social/android/quick-integration的报错必看，正式发布，请关闭该模式
    }

    //开启倒计时  两分钟无响应就跳转动到首页
    private void startCountDown() {
        if (mHandler == null) {
            mHandler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    backHomeActivity();
                    resetCamera();
                }
            };
        }
        mHandler.sendEmptyMessageDelayed(0, delayTime);
    }

    public void backHomeActivity() {
        for (Activity activity : activityList) {
            if (!(activity instanceof MainActivity) &&
                    !(activity instanceof LoginActivity) &&
                    !(activity instanceof TestActivity) &&
                    !(activity instanceof CarEvaluateQueryActivity) &&
                    !(activity instanceof ForgetPasswordActivity)) {
                activity.finish();
            }
            resetHomeUi(activity);
        }
        resetCountDown();
    }


    private void resetHomeUi(Activity activity) {
        if (activity instanceof CarEvaluateQueryActivity) {
            CarEvaluateQueryActivity carEvaluateQueryActivity = (CarEvaluateQueryActivity) activity;
            carEvaluateQueryActivity.reSetUi();
        }
    }

    public void logout() {
        for (Activity activity : activityList) {
            if (!(activity instanceof MainActivity) &&
                    !(activity instanceof LoginActivity) &&
                    !(activity instanceof TestActivity) &&
                    !(activity instanceof ForgetPasswordActivity)) {
                activity.finish();
            }
        }
        resetCountDown();
    }

    public void resetCountDown() {
        Log.e("reset", "重置");
        mHandler.removeMessages(0);
        mHandler.sendEmptyMessageDelayed(0, delayTime);
    }

    private void initWithHeight() {
        WindowManager wm = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);

        G.width = wm.getDefaultDisplay().getWidth();
        G.height = wm.getDefaultDisplay().getHeight();
    }

    private void initOtherSdk() {
//        //打印日志
//        JLog.init(this).setDebug(BuildConfig.DEBUG);
        //数据库
        LitePal.initialize(this);
        //bug提交
        CrashReport.initCrashReport(getApplicationContext(), "0b6859f563", true);
    }

    /**
     * 分割 Dex 支持
     *
     * @param base
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
//        MultiDex.install(this);
    }


    public String getVersion() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "1.0.0";
        }
    }

    /**
     * 应用退出，结束所有的activity
     */
    public void exit() {
        for (Activity activity : activityList) {
            if (activity != null) {
                activity.finish();
            }
        }
        System.exit(0);
    }

    public void cancelCamera() {
        for (Activity activity : activityList) {
            if (activity instanceof CarEvaluateQueryActivity) {
                ((CarEvaluateQueryActivity) activity).cancelReStartCamera();
            }
        }
    }

    public void resetCamera() {
        for (Activity activity : activityList) {
            if (activity instanceof CarEvaluateQueryActivity) {
                ((CarEvaluateQueryActivity) activity).reSetCamera();
            }
        }
    }
}
